<template>
  <div class="selCourseContainer">
    <BasicTablePagination @handler="tableHandler" :data="data" :column="column" :pagination="pagination" @sizeChange="sizeChange" @numChange="numChange"></BasicTablePagination>
  </div>
</template>

<script>
import BasicTablePagination from '@/components/BasicTablePagination/BasicTablePagination'
import { tableColumn } from '@/constants/selCourse'
import { getSchoolCourse, choseCourse } from '@/api/user'

export default {
  name: "index",
  components: {
    BasicTablePagination
  },
  data() {
    return {
      column: tableColumn,
      data: [],
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  methods: {
    tableHandler(val) {
      let { type, data } = val
      console.log(type, data)
      switch(type) {
        case 'chose':
          this.choseCourse(data)
          break
        default:
          break
      }
    },
    async getData() {
      const res = await getSchoolCourse(this.pagination)
      if (res.success == true) {
        this.data = res.data
        this.pagination.total = res.total
      } else {
        this.$message.error(res.errorMsg)
      }
    },
    sizeChange(val) {
      this.pagination.pageSize = val
      this.getData()
    },
    numChange(val) {
      this.pagination.pageNum = val
      this.getData()
    },
    async choseCourse(val) {
      const res = await choseCourse(val.courseId)
      if (res.success == true) {
        this.$message.success('选课成功！')
      } else {
        this.$message.error(res.errorMsg)
      }
    }
  },
  created() {
    this.getData()
  }
}
</script>

<style scoped lang="sass">
.selCourseContainer
  padding: 20px
</style>
